using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace CSharpPro._0930;

public class User
{
    private Dictionary<string, string> _userAccounts = new Dictionary<string, string>();
    //用户注册
    public void Show()
    {
        bool isRunning = true;
        
        while (isRunning)
        {
            Console.Clear();
            Console.WriteLine("===== 欢迎使用 =====");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.WriteLine("====================");
            Console.Write("请选择(1-3): ");
            
            string input = Console.ReadLine()?.Trim();
            
            // 处理菜单选择
            if (string.IsNullOrEmpty(input))
            {
                Console.WriteLine("请输入有效数字！");
                WaitForKeyPress();
                continue;
            }
            
            switch (input)
            {
                case "1":
                    Login();
                    break;
                case "2":
                    Regist();
                    break;
                case "3":
                    isRunning = false;
                    Console.WriteLine("程序已退出，感谢使用！");
                    break;
                default:
                    Console.WriteLine("请输入有效数字！");
                    WaitForKeyPress();
                    break;
            }
        }
    }
    //用户登录
    public void Login()
    {
        Console.Clear();
        Console.WriteLine("===== 用户登录 =====");
        
        // 获取并验证用户名
        Console.Write("请输入用户名: ");
        string username = Console.ReadLine()?.Trim();
        
        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空！");
            WaitForKeyPress();
            return;
        }
        
        // 获取并验证密码
        Console.Write("请输入密码: ");
        string password = Console.ReadLine()?.Trim();
        
        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空！");
            WaitForKeyPress();
            return;
        }
        
        // 验证用户是否存在
        if (!_userAccounts.ContainsKey(username))
        {
            Console.WriteLine("登录失败：用户名不存在！");
            WaitForKeyPress();
            return;
        }
        
        // 验证密码是否正确
        if (_userAccounts[username] == password)
        {
            Console.WriteLine($"登录成功！欢迎您，{username}！");
            Console.WriteLine("正在进入系统首页...");
            WaitForKeyPress();
        }
        else
        {
            Console.WriteLine("登录失败：密码错误！");
            WaitForKeyPress();
        }
    }
    //主菜单交互
    public void Regist()
    {
         Console.Clear();
        Console.WriteLine("===== 用户注册 =====");
        
        // 验证用户名
        string username = GetValidUsername();
        if (string.IsNullOrEmpty(username))
        {
            WaitForKeyPress();
            return;
        }
        
        // 验证密码
        string password = GetValidPassword();
        if (string.IsNullOrEmpty(password))
        {
            WaitForKeyPress();
            return;
        }
        
        // 确认密码
        if (!ConfirmPassword(password))
        {
            WaitForKeyPress();
            return;
        }
        
        // 完成注册
        _userAccounts.Add(username, password);
        Console.WriteLine("注册成功！即将返回主菜单...");
        WaitForKeyPress();
    }
    
    // 获取并验证用户名
    private string GetValidUsername()
    {
        while (true)
        {
            Console.Write("请设置用户名: ");
            string username = Console.ReadLine()?.Trim();
            
            // 验证非空
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不可为空！");
                continue;
            }
            
            // 验证长度
            if (username.Length < 3 || username.Length > 15)
            {
                Console.WriteLine("用户名长度必须为3-15个字符！");
                continue;
            }
            
            // 验证字符组成
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字和下划线！");
                continue;
            }
            
            // 验证用户名唯一性
            if (_userAccounts.ContainsKey(username))
            {
                Console.WriteLine("该用户名已被注册，请更换！");
                continue;
            }
            
            return username;
        }
    }
    
    // 获取并验证密码
    private string GetValidPassword()
    {
        while (true)
        {
            Console.Write("请设置密码: ");
            string password = Console.ReadLine()?.Trim();
            
            // 验证非空
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不可为空！");
                continue;
            }
            
            // 验证长度
            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度必须为6-20个字符！");
                continue;
            }
            
            // 验证密码复杂度
            bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
            bool hasLower = Regex.IsMatch(password, @"[a-z]");
            bool hasDigit = Regex.IsMatch(password, @"[0-9]");
            
            if (!hasUpper || !hasLower || !hasDigit)
            {
                Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                continue;
            }
            
            return password;
        }
    }
    
    // 确认密码
    private bool ConfirmPassword(string password)
    {
        while (true)
        {
            Console.Write("请确认密码: ");
            string confirmPassword = Console.ReadLine()?.Trim();
            
            if (string.IsNullOrEmpty(confirmPassword))
            {
                Console.WriteLine("确认密码不可为空！");
                continue;
            }
            
            if (confirmPassword != password)
            {
                Console.WriteLine("两次输入的密码不一致，请重新输入！");
                continue;
            }
            
            return true;
        }
    }
    
    // 等待用户按键继续
    private void WaitForKeyPress()
    {
        Console.WriteLine("按任意键继续...");
        Console.ReadKey();
    }
}
